﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery-1.6.1-vsdoc.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.ajaxmanager-3.12.debug.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.json.debug.js"/>
/// <reference path="~/Clients/Scripts/globals.debug.js"/>
/// <reference path="~/Clients/Scripts/utils.debug.js"/>
/// <reference path="~/Clients/Scripts/common.debug.js"/>
/// <reference path="~/Clients/Scripts/stacktrace.debug.js"/>


Type.registerNamespace("GreeNova");

GreeNova.ErrorHandling = new function () {
    this.NavigateToErrorPage = function () {
        try {
            debugger;
            window.location.href = GreeNova.Globals.resolveUrl('ErrorPages/Info.aspx');
        }
        catch (e) { }
    };

    this._LogErrorToWS = function (info, gotoErrorPage) {
        try {
            if (!info)
                return;
            var t;
            if (gotoErrorPage)
                t = setTimeout(function () { this.NavigateToErrorPage(); } .setCtx(this), 2000);
            $.ajax({
                type: "POST",
                url: GreeNova.Globals.resolveUrl('Clients/ws/ErrorLogging.asmx/LogError'),
                cache: false,
                timeout: 2000,
                data: Sys.Serialization.JavaScriptSerializer.serialize({
                    info: info
                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function () {
                } .setCtx(this),
                complete: function (xhr, status) {
                    if (t)
                        clearTimeout(t);
                    if (gotoErrorPage)
                        this.NavigateToErrorPage();
                } .setCtx(this)
            });
        }
        catch (e) { }
    };

    this._checkOpt = function (opt) {
        if (!opt)
            opt = {};
        if (!opt.data)
            opt.data = {};
        return opt;
    };

    this.Handle = function (opt) {
        debugger;
        try {
            if (!opt)
                return;
            var err = {};
            if (opt.id) {
                try {
                    err.Id = Sys.Serialization.JavaScriptSerializer.serialize(opt.id);
                }
                catch (e) { }
            }
            if (opt.data) {
                try {
                    err.Data = Sys.Serialization.JavaScriptSerializer.serialize(opt.data);
                }
                catch (e) { }
            }
            if (opt.msg) {
                try {
                    err.Message = Sys.Serialization.JavaScriptSerializer.serialize(opt.msg);
                }
                catch (e) { }
            }
            if (opt.ex) {
                try {
                    err.Exception = Sys.Serialization.JavaScriptSerializer.serialize(opt.ex);
                }
                catch (e) { }
            }
            if (opt.stack) {
                try {
                    err.Stack = printStackTrace();
                }
                catch (e) { }
            }
            var gotoErrorPage = !!opt.redir;
            this._LogErrorToWS(err, gotoErrorPage);
        }
        catch (e) { }
    };

    //this.HandleError = function(info, gotoErrorPage, logStackTrace) {
    //    try {
    //        var err = {
    //            error: info
    //        };
    //        if (logStackTrace === undefined || logStackTrace) {
    //            try {
    //                err.trace = printStackTrace();
    //            }
    //            catch (e) { }
    //        }
    //        this._LogErrorToWS(err, gotoErrorPage);
    //    }
    //    catch (e) { }
    //};

    this.HandleStatusCodeError = function (error, statusCode, opt) {
        debugger;
        try {
            if (/*statusCode == '0' ||*/statusCode == '401' || statusCode == '12030' || statusCode == '12031') {
                window.location = window.location;
                window.location.reload();
                return true;
            }
            opt = this._checkOpt(opt);
            opt.data.error = error;
            opt.data.statusCode = statusCode;

            if (statusCode == '0') {
                this.Handle(opt);
                return true;
            }
            opt.redir = true;
            this.Handle(opt);
        }
        catch (e) { }
        return false;
    };

    //this.ServiceError = function(error) {
    //    try {
    //        if (typeof (error) !== 'undefined' && typeof (error.get_statusCode) !== 'undefined') {
    //            var statusCode = error.get_statusCode();
    //            if (this.HandleStatusCodeError(error, statusCode))
    //                return;
    //        }
    //        else
    //            this.HandleError(error, true);
    //    }
    //    catch (e) { }
    //};

    //this.PageMethodError = function(error) {
    //    try {
    //        if (typeof (error) !== 'undefined' && typeof (error.get_statusCode) !== 'undefined') {
    //            var statusCode = error.get_statusCode();
    //            if (this.HandleStatusCodeError(error, statusCode))
    //                return;
    //        }
    //    }
    //    catch (e) { }
    //    this.HandleError(error, true);
    //};

    this.jqAMError = function (xhr, amOpt, opt) {
        debugger;
        try {
            opt = this._checkOpt(opt);
            opt.data.amOpt = amOpt;
            if (xhr && xhr.status && xhr.responseText) {
                if (this.HandleStatusCodeError(xhr.responseText, xhr.status, opt))
                    return;
            }
            else {
                opt.redir = true;
                this.Handle(opt);
            }
        }
        catch (e) { }
    };

    //this.jqAjaxError = function(xhr, opt) {
    //    try {
    //        if (xhr && xhr.status && xhr.responseText) {
    //            if (this.HandleStatusCodeError(xhr.responseText, xhr.status, opt))
    //                return;
    //        }
    //        else {
    //            opt = this._checkOpt(opt);
    //            opt.data.xhr = xhr;
    //            opt.redir = true;
    //            this.Handle(opt);
    //        }
    //    }
    //    catch (e) { }
    //};
};
